<?php
abstract class Shinkensaku_Model_Base extends Apm_Model_Base{

    protected $strCheckSQLT57 = '';
	protected $strCheckSQLT03 = '';
    protected $strCheckSQLM43 = '';
    protected $strCheckSQLM03 = '';
    protected $strWhere1 = '';
    protected $strWhere2 = '';

    /**
     * sql文を発行し、データベースからデータを取得（複数）
     * @param $params
     */
    public function getList($params)
    {
        $db_params = array();
        $sql = $this->generateSql($params, $db_params);
        return $this->dbGetList($sql, $db_params);
    }

    /**
     * sql文を発行し、データベースからデータを取得（複数「OEM用」）
     * @param $params
     */
    public function getOemList($params)
    {
        $db_params = array();
        $sql = $this->generateOemSql($params, $db_params);
        return $this->dbGetList($sql, $db_params);
    }

    /**
     * 都道府県コードが存在する場合、条件文を生成
     * @param $params
     * @return なし
     */
    public function setCondPrefCd($params)
    {
	    $this->strCheckSQLT57 = '';
    	$this->strCheckSQLT03 = '';
	    $this->strCheckSQLM43 = '';
    	$this->strCheckSQLM03 = '';
	    $this->strWhere1 = '';
	    $this->strWhere2 = '';

    	$PrefCdList = array();

	    // 都道府県コード取得
    	if (count($params['PrefCd']) > 0)
	    {
	        foreach (array_filter(array_unique($params['PrefCd'])) as $PrefCd)
	        {
		        $PrefCdList[] = $PrefCd;
	        }
    	}

        if (count($PrefCdList) > 0)
        {
	        $PrefCds = "'".join("','", $PrefCdList)."'";

	        $this->strCheckSQLT57 = " T57_TODOUFUKEN_CD IN ({$PrefCds}) AND ";
	        $this->strCheckSQLT03 = " T03_TODOUFUKEN_CD IN ({$PrefCds}) AND ";
	        $this->strCheckSQLM43 = " WHERE M43_TODOUFUKEN_CD IN ({$PrefCds}) ";
	        $this->strCheckSQLM03 = " AND M03_TODOUFUKEN_CD IN ({$PrefCds}) ";

	        $this->strWhere1 = " T03_TODOUFUKEN_CD IN ({$PrefCds}) ";
	        $this->strWhere2 = " M03_TODOUFUKEN_CD IN ({$PrefCds}) ";
        }
    }

    /**
     * OemCｄに基づく条件を生成
     * @param $params
     * @return sql条件文
     */
    protected function generateCondOem($params)
    {
        $strWhereOemM05 = '';
        $OemCd = $params['OemCd'];

        // OEM桁が8桁(=店舗)であった場合
        if(strlen($OemCd) == 8)
        {
            $strWhereOemM05 .= " AND ( M05_TENPO_CD = '{$OemCd}' ) ";

            // 物件検索から来た場合はOEMチェックしない
		    if (array_key_exists('IsbukenList', $params) == false || $params['IsbukenList'] != 1)
            {
            	$strWhereOemM05 .= " AND ( M05_OEM IN (2, 3)) ";
            }
        }
        // OEM桁が4桁(=法人)であった場合
        else
        {
            $strWhereOemM05 = " AND ( M05_HOUZIN_CD = '{$OemCd}' ) ";
         	
        	// 2011/01/29 加盟店TOP対応
		    if (array_key_exists('IsbukenList', $params) == false || $params['IsbukenList'] != 1)
            {
            	$strWhereOemM05 .= "AND ( M05_OEM IN (1, 3)) ";
            }
        }

        return $strWhereOemM05;
    }

	/**
	 * 参照区分チェック
	 * @param $param
	 */
	public function ChkRefKbn($param)
	{
		$intGoFlg = 0;

		// OemCdがある場合
		if (array_key_exists('OemCd', $param) == true || strcmp($param['OemCd'], '') != 0)
		{
			// OemCd保持
			$strOemCd = $param['OemCd'];
			$strWork_M05 = '';

			if (mb_strlen($strOemCd) == 8)
			{
				// 店舗
				$strWork_M05 = "M05_TENPO_CD = '{$strOemCd}'";

				// 物件検索から来た場合はチェックしない
		    	if (array_key_exists('IsbukenList', $param) == false || $param['IsbukenList'] != 1)
		        {
					$strWork_M05 .= " AND ( M05_OEM = 2 OR M05_OEM = 3 ) ";
		        }
			}
			else
			{
				// 法人
				$strWork_M05 = "M05_HOUZIN_CD = '{$strOemCd}' ";
				
		    	if (array_key_exists('IsbukenList', $param) == false || $param['IsbukenList'] != 1)
		        {
					$strWork_M05 .= "AND ( M05_OEM = 1 OR M05_OEM = 3 ) ";
				}
				
			}

			// クエリ作成（指定法人のﾃﾞｰﾀを取得する）
$sql = <<< docEnd
				SELECT
					NVL(COUNT(*),0) AS EXIST_FLG
				FROM M05TENPO_INFO
				WHERE {$strWork_M05}
docEnd;

			$db_params = array();
        	$exist_flg = $this->dbGetData($sql, $db_params);

        	if ($exist_flg['EXIST_FLG'] == 0)
        	{
        		$intGoFlg = 1;
        	}
		}

		return $intGoFlg;
	}

}
?>
